Introduction

Recently, I came across the nice little library xmldataset that allows you to flatten multi-level XML data so that you can process it easily with Pandas.

pip install xmldataset

In [37]:
import xmldataset
import pandas as pd
xml = ""

with open ("datasets/jacocoTestReport.xml") as f:
    xml = f.read()


profile="""
report
    package
        class
            name = external_dataset:info,prefix:class_
            method
                name = external_dataset:info,prefix:method_
                counter
                    type = dataset:measures
                    missed = dataset:measures
                    covered = dataset:measures
                    __EXTERNAL_VALUE__ = info:class_name:measures info:method_name:measures
                    
"""


output = xmldataset.parse_using_profile(xml,profile)
coverage_data = pd.DataFrame.from_records(output['measures'])
coverage_data.head()


Out[37]:
class_name covered method_name missed type
0 org/springframework/samples/petclinic/Petclini... 3 <init> 0 INSTRUCTION
1 org/springframework/samples/petclinic/Petclini... 1 <init> 0 LINE
2 org/springframework/samples/petclinic/Petclini... 1 <init> 0 COMPLEXITY
3 org/springframework/samples/petclinic/Petclini... 1 <init> 0 METHOD
4 org/springframework/samples/petclinic/Petclini... 7 onStartup 0 INSTRUCTION

In [44]:
coverage_data['covered'] = pd.to_numeric(coverage_data['covered'])

In [56]:
coverage_data.pivot_table(
    index=["class_name", "method_name"],
    columns=['type'],
    values=["covered", "missed"])


Out[56]:
covered
type BRANCH COMPLEXITY INSTRUCTION LINE METHOD
class_name method_name
org/springframework/samples/petclinic/PetclinicInitializer <init> NaN 1.0 3.0 1.0 1.0
createRootApplicationContext NaN 1.0 27.0 4.0 1.0
createServletApplicationContext NaN 1.0 9.0 3.0 1.0
getServletFilters NaN 1.0 29.0 4.0 1.0
getServletMappings NaN 1.0 7.0 1.0 1.0
onStartup NaN 1.0 7.0 3.0 1.0
registerDandelionServlet NaN 1.0 22.0 5.0 1.0
registerServletFilter NaN 1.0 20.0 3.0 1.0
org/springframework/samples/petclinic/model/BaseEntity <init> NaN 1.0 3.0 1.0 1.0
getId NaN 1.0 3.0 1.0 1.0
isNew 2.0 2.0 7.0 1.0 1.0
setId NaN 1.0 4.0 2.0 1.0
org/springframework/samples/petclinic/model/NamedEntity <init> NaN 1.0 3.0 1.0 1.0
getName NaN 1.0 3.0 1.0 1.0
setName NaN 1.0 4.0 2.0 1.0
toString NaN 0.0 0.0 0.0 0.0
org/springframework/samples/petclinic/model/Owner <init> NaN 1.0 3.0 1.0 1.0
addPet NaN 1.0 9.0 3.0 1.0
getAddress NaN 1.0 3.0 1.0 1.0
getCity NaN 1.0 3.0 1.0 1.0
getPet 5.0 1.0 16.0 4.0 0.5
getPets NaN 1.0 17.0 3.0 1.0
getPetsInternal 1.0 1.0 6.0 2.0 1.0
getTelephone NaN 1.0 3.0 1.0 1.0
setAddress NaN 1.0 4.0 2.0 1.0
setCity NaN 1.0 4.0 2.0 1.0
setPetsInternal NaN 0.0 0.0 0.0 0.0
setTelephone NaN 1.0 4.0 2.0 1.0
toString NaN 0.0 0.0 0.0 0.0
org/springframework/samples/petclinic/model/Person <init> NaN 1.0 3.0 1.0 1.0
... ... ... ... ... ... ...
org/springframework/samples/petclinic/web/OwnerController initUpdateOwnerForm NaN 1.0 11.0 3.0 1.0
processCreationForm 0.0 0.0 0.0 0.0 0.0
processFindForm 3.0 1.0 23.0 6.0 1.0
processUpdateOwnerForm 1.0 1.0 13.0 4.0 1.0
setAllowedFields NaN 1.0 9.0 2.0 1.0
showOwner NaN 1.0 14.0 3.0 1.0
org/springframework/samples/petclinic/web/PetController <init> NaN 1.0 6.0 3.0 1.0
findOwner NaN 1.0 5.0 1.0 1.0
initCreationForm NaN 1.0 14.0 4.0 1.0
initOwnerBinder NaN 1.0 9.0 2.0 1.0
initPetBinder NaN 1.0 6.0 2.0 1.0
initUpdateForm NaN 0.0 0.0 0.0 0.0
populatePetTypes NaN 1.0 4.0 1.0 1.0
processCreationForm 4.0 1.0 25.0 5.0 1.0
processUpdateForm 0.0 0.0 0.0 0.0 0.0
org/springframework/samples/petclinic/web/PetTypeFormatter <init> NaN 1.0 6.0 3.0 1.0
parse 3.0 2.0 22.0 5.0 1.0
print NaN 1.0 3.0 1.0 1.0
org/springframework/samples/petclinic/web/PetValidator <init> NaN 1.0 3.0 1.0 1.0
supports NaN 1.0 4.0 1.0 1.0
validate 4.0 1.0 19.0 6.0 1.0
org/springframework/samples/petclinic/web/VetController <init> NaN 1.0 6.0 3.0 1.0
showResourcesVetList NaN 0.0 0.0 0.0 0.0
showVetList NaN 0.0 0.0 0.0 0.0
org/springframework/samples/petclinic/web/VisitController <init> NaN 1.0 6.0 3.0 1.0
initNewVisitForm NaN 1.0 2.0 1.0 1.0
loadPetWithVisit NaN 1.0 14.0 4.0 1.0
processNewVisitForm 1.0 1.0 9.0 3.0 1.0
setAllowedFields NaN 1.0 9.0 2.0 1.0
showVisits NaN 0.0 0.0 0.0 0.0

162 rows × 5 columns

Limitations

Unfortunately, there is currently an important feature missing. Apparently, you